const app = getApp()
import { caddieLogin } from '../../http/caddie'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        currentData: 0,
        selectPerson: true,
        staticUrl: app.globalData.staticUrl,
        account: '',
        password: '',
        matchid:'',
    },
    pageData: { //不渲染到页面的全局变量
        match_id: '',
        round_id: '',
        group_id: ''
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if(options.scene){
            this.setData({matchid:options.scene});
            this.pageData = { match_id:options.scene }
        }
        
        let { match_id, round_id, group_id } = options
        this.pageData = { match_id, round_id, group_id }

        var caddieInfo =  wx.getStorageSync('caddieInfo');
        console.log(caddieInfo);
        if(caddieInfo){  //查询是否存在
          
            if(new Date(caddieInfo.logintime).toDateString() === new Date().toDateString() ){
              console.log(caddieInfo);
                 let{account,password} = this.data;
                 account  = caddieInfo.ca_account;
                 password = caddieInfo.ca_password;
                 if(match_id && match_id !=  caddieInfo.match_id){
                   match_id = match_id;
                 }
                 else if(!match_id && caddieInfo.match_id){
                   match_id = caddieInfo.match_id;
                 }
                 this.setData({matchid:match_id, account, password})
            }
            else{
                if(caddieInfo.logintime){
                  wx.setStorageSync('caddieInfo','');
                }
            }
            }
    },
    // 登录
    login() {
        let { match_id, round_id, group_id } = this.pageData
        const params = {
            ca_account: this.data.account,
            ca_password: this.data.password,
            match_id:this.data.matchid,
        }
        if(!params.match_id){
            wx.showToast({
              title: '赛事代码为空',
              icon:'error'
            })
            return;
        }
        if(!params.ca_account){
            wx.showToast({
              title: '记分员账号为空',
              icon:'error'
            })
            return;
        }
        if(!params.ca_password){
            wx.showToast({
              title: '密码为空',
              icon:'error'
            })
            return;
        }
        caddieLogin(params).then(res => {
            console.log(res);
            if (res.errorcode.code  == 'ok') {
               
                group_id = res.data.gp_id;
                round_id =  res.data.round_id;
                match_id =  res.data.match_id;
                res.data.ca_account = this.data.account;
                res.data.ca_password= this.data.password;
                res.data.logintime = new Date();
                wx.setStorageSync('caddieInfo', res.data)
                wx.redirectTo({
                    url: `./scoringpage/scoringpage?match_id=${match_id}&round_id=${round_id}&group_id=${group_id}`,
                })
            }
        })
    },
    // 输入记分员账号
    bindAccountChange: function(e) {
        this.setData({ account: e.detail.value })
    },
    // 输入密码
    bindPasswordChange: function(e) {
        this.setData({ password: e.detail.value })
    },

     // 输入密码
     bindmatchidChange: function(e) {
        this.setData({ matchid: e.detail.value })
    },

    //获取当前滑块的index
    bindchange: function(e) {
        const that = this;
        that.setData({
            currentData: e.detail.current
        })
    },
    //点击切换，滑块index赋值
    checkCurrent: function(e) {
        const that = this;

        if (that.data.currentData === e.target.dataset.current) {
            return false;
        } else {

            that.setData({
                currentData: e.target.dataset.current
            })
        }
    },
    scan:function() {
        var that = this;
        wx.scanCode({
          
          scanType:'qrCode',
          success:function(res) {
            if(res.path){
                var matchidarr = res.path.split('?')
                if(matchidarr.length>1){
                   var matchid =  matchidarr[1].split('=')[1];
                    console.log(matchid);
                    that.setData({matchid:matchid})
                    that.pageData.match_id = matchid;
                }
                
            }
          },fail:function(res) {
                console.log(res);
          }
        })
    },
    goback:function() {
        wx.navigateBack();
    }
})